package me.baicai.modules.belldandy.gcb.rest;

import me.baicai.aop.log.Log;
import me.baicai.modules.belldandy.gcb.domain.CrmOrder;
import me.baicai.modules.belldandy.gcb.service.CrmOrderService;
import me.baicai.modules.belldandy.gcb.service.dto.CrmOrderQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author 超级大白菜
* @date 2020-04-17
*/
@Api(tags = "CrmOrder管理")
@RestController
@RequestMapping("/api/crmOrder")
public class CrmOrderController {

    private final CrmOrderService crmOrderService;

    public CrmOrderController(CrmOrderService crmOrderService) {
        this.crmOrderService = crmOrderService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    public void download(HttpServletResponse response, CrmOrderQueryCriteria criteria) throws IOException {
        crmOrderService.download(crmOrderService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询CrmOrder")
    @ApiOperation("查询CrmOrder")
    public ResponseEntity getCrmOrders(CrmOrderQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(crmOrderService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增CrmOrder")
    @ApiOperation("新增CrmOrder")
    public ResponseEntity create(@Validated @RequestBody CrmOrder resources){
        return new ResponseEntity<>(crmOrderService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改CrmOrder")
    @ApiOperation("修改CrmOrder")
    public ResponseEntity update(@Validated @RequestBody CrmOrder resources){
        crmOrderService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping(value = "/{id}")
    @Log("删除CrmOrder")
    @ApiOperation("删除CrmOrder")
    public ResponseEntity delete(@PathVariable String id){
        crmOrderService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}
